package pl.edu.agh.sius;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


class Application{
	protected String address;
	protected static URL wsdlURL;
	protected static QName serviceName;
	protected static QName portName;
	protected static org.mortbay.jetty.Server server;	
	
	public Application(String address, int port){
	      this.address = address;
	      try {
			wsdlURL = new URL(address + "?wsdl");
		  } catch (MalformedURLException e) {
			  e.printStackTrace();
		  }
	      serviceName = new QName("http://sius.agh.edu.pl/wsdl/SimpleP2PServer.wsdl", "SimpleP2PServerService");
	      portName = new QName("http://sius.agh.edu.pl/wsdl/SimpleP2PServer.wsdl", "SimpleP2PServerPort");
	      
	      server = new Server();
	      Connector connector = new SelectChannelConnector();
	      connector.setPort(port);
	      server.setConnectors(new Connector[] {connector});
	      
	      WebAppContext wactx = new WebAppContext();
	      wactx.setParentLoaderPriority(true);
	      wactx.setContextPath("/simple-server");
	      wactx.setWar("simple-server.war");

	      HandlerCollection handlers = new HandlerCollection();
	      handlers.setHandlers(new Handler[] {wactx, new DefaultHandler()});
	      server.setHandler(handlers);

		
	}
	
	private void start() {
	      try {
	          server.start();
	      } catch (Exception e) {
	          e.printStackTrace();
	      }   
	}	
	
	
	
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

		Application application = (Application) context.getBean("application");
		application.start();
	}
}